package com.hanhai.zrb.backend.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.hanhai.common.annotation.PermissionNeed;
import com.hanhai.common.web.controller.BaseController;
import com.hanhai.zrb.backend.admin.model.dto.RoleDto;
import com.hanhai.zrb.backend.admin.service.PrivilegeService;
import com.hanhai.zrb.backend.admin.service.RoleService;

@Controller
@RequestMapping("role")
public class RoleController extends BaseController {
	
	@Resource
	private RoleService roleService;

	@Resource
	private PrivilegeService privilegeService;

	@PermissionNeed("privilege_role_list")
	@RequestMapping("")
	public String index(){
		return "role/list";
	}
	
	@PermissionNeed("privilege_role_list")
	@RequestMapping("list")
	public void list(Model model, HttpServletResponse response) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> list = roleService.listAll();
		int total = list.size();
		map.put("rows", list);
		map.put("total", total);
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}

	@RequestMapping("initPrivilege")
	public void initPrivilege(@RequestParam(value="id",required = false) Integer id,HttpServletResponse response) throws IOException {
		List<Map<String, Object>> privilegeList = privilegeService.listAll();
		if(null!=id){
			List<Map<String, Object>> ownPrivilegeList = privilegeService
					.listByRole(id);
			for (Map<String, Object> p : privilegeList) {
				boolean contains = contains(ownPrivilegeList, p);
				if (contains) {
					p.put("checked", "checked");
				}
			}
		}
		String str=JSONObject.toJSONString(privilegeList).replaceAll("name", "text");
		response.getWriter().write(str);
	}
	

	@PermissionNeed("privilege_role_management")
	@RequestMapping("edit")
	public String edit(@RequestParam Integer id, Model model) {
		Map<String, Object> role = roleService.getDetail(id);
		model.addAttribute("role", role);
		// TODO 浼樺寲锛岃浆鎹㈡垚key-map
		List<Map<String, Object>> ownPrivilegeList = privilegeService
				.listByRole(id);
		List<Map<String, Object>> privilegeList = privilegeService.listAll();
		for (Map<String, Object> p : privilegeList) {
			boolean contains = contains(ownPrivilegeList, p);
			if (contains) {
				p.put("checked", "checked");
			}
		}
		model.addAttribute("privilegeList", privilegeList);

		return "role/edit";
	}

	private boolean contains(List<Map<String, Object>> ownPrivilegeList,
			Map<String, Object> p) {
		for (Map<String, Object> own : ownPrivilegeList) {
			if (own != null && own.get("acl").equals(p.get("acl"))) {
				return true;
			}
		}
		return false;
	}

	@PermissionNeed("privilege_role_management")
	@RequestMapping("doadd")
	public void doadd(RoleDto params, Model model, HttpServletRequest request, HttpServletResponse response) {
		params.strToArray();
		roleService.add(params);
		super.jsonSucceed(response);
	}

	

	@PermissionNeed("privilege_role_management")
	@RequestMapping("doupdate")
	public void doupdate(RoleDto params, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		params.strToArray();
		roleService.update(params);
		super.jsonSucceed(response);
	}


	@PermissionNeed("privilege_role_management")
	@RequestMapping("delete")
	public void delete(Integer id, Model model, HttpServletRequest request, HttpServletResponse response) throws IOException {
		boolean succeed = roleService.delete(id);
		Map<String, Object> map=new HashMap<String, Object>();
		if(succeed){
			map.put("success", true);
		}else{
			map.put("success", false);
			map.put("infoMsg", "璇ヨ鑹蹭笅瀛樺湪鐢ㄦ埛");
		}
		String str=JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}

}
